package com.atguigu.gmall.mq.listenner;

import com.atguigu.gmall.common.config.DeadLetterConfig;
import com.atguigu.gmall.common.config.DelayedConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @author gsy
 * @date 2021年09月12日21:05
 * Description
 */
@Component
public class ConsumerListenner {

    @RabbitListener(queues = "queue123")
    public void receiverMessage(String msg, Channel channel,Message message){
        System.out.println("接受到的消息"+new String(message.getBody()));
        System.out.println("接受到的消息"+msg);
        //手动应答
        //参数1:消息的唯一标识
        //参数2:是否批量确实
        try {
           // int i =1/0;
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
           // e.printStackTrace();
            if (!message.getMessageProperties().isRedelivered()){
                //第一次接受消息 给一次重新发生的机会
                //参数3:重新返回队列
                try {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else {
                //重试消息,重新发送消息机会用完
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }
    //接受延迟消息  基于死信
    @RabbitListener(queues = DeadLetterConfig.queue_dead_2)
    public void receiverDeadLetterMessage(String msg, Channel channel,Message message){
        //System.out.println("接受到的消息"+new String(message.getBody()));
        System.out.println("接受到的消息:"+msg+",接受时间:"+new Date());
        //手动应答
        //参数1:消息的唯一标识
        //参数2:是否批量确实
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            // e.printStackTrace();
            if (!message.getMessageProperties().isRedelivered()){
                //第一次接受消息 给一次重新发生的机会
                //参数3:重新返回队列
                try {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else {
                //重试消息,重新发送消息机会用完
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }


    //接受延迟消息  基于插件
    @RabbitListener(queues = DelayedConfig.queue_delay_1)
    public void receiverDelayedMessage(String msg, Channel channel,Message message){
        //System.out.println("接受到的消息"+new String(message.getBody()));
        System.out.println("接受到的消息:"+msg+",接受时间:"+new Date());
        //手动应答
        //参数1:消息的唯一标识
        //参数2:是否批量确实
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            // e.printStackTrace();
            if (!message.getMessageProperties().isRedelivered()){
                //第一次接受消息 给一次重新发生的机会
                //参数3:重新返回队列
                try {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }else {
                //重试消息,重新发送消息机会用完
                try {
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }
}
